"""
作者：李明珠
功能：芯片平均间距计算
版本:1.0
日期：2020-09-30
"""
import pandas as pd
from sqlalchemy import create_engine

# 读取‘基板代码表’查询基板信息。
# data = pd.DataFrame (pd.read_excel('LM-F302001-1 基板代码表20200907.xlsx'))
engine = create_engine("mssql+pyodbc://pereader:pereader@172.18.65.31:1433/SortingDB?"
                       "driver=ODBC+Driver+17+for+SQL+Server", fast_executemany=True)
Substrate_code = input("请输入基板代码:")
data = pd.read_sql(f"SELECT * FROM RD_Substrate_Code WHERE Substrate_Code='{Substrate_code}' AND DataStatus=1", engine)
# Substrate_code1 = eval(Substrate_code)
# result = data.loc[data['基板代码'] == Substrate_code]
result = data[['ID','Substrate_Code','LES_mm']]
print(result)

# 输入镜面直径、串并数、芯片长宽、围坝层数（1为常规围坝，2为双层围坝）信息
diameter = input("请输入镜面直径单位mm:")
String = input("请输入串数:")
Parallel = input("请输入并数:")
chip_length = input("请输入芯片长单位mil:")
chip_wide = input("请输入芯片宽单位mil:")
Dam_Dispensing = input("请输入层数:")

# 将镜面直径、串并数、芯片长宽信息转换成数值
D = eval(diameter)
S = eval(String)
P = eval(Parallel)
chip_L = eval(chip_length)
chip_W = eval(chip_wide)
Dam = eval(Dam_Dispensing)

# 计算芯片面积、芯片总数、镜面面积、芯片总面积占镜面面积之比、镜面偏移、有效面积、芯片间距
chip_area = chip_L * 0.0254 * chip_W * 0.0254
chip_sum = S * P
D_area = pow(D / 2, 2) * 3.14
chip_than = chip_area * chip_sum / D_area

if Dam == 1 and chip_than > 0.5:
    D_Offset = 0.2
elif Dam == 1 and 0.3 < chip_than <= 0.5:
    D_Offset = 0.3
elif Dam == 1 and 0.23 < chip_than <= 0.3:
    D_Offset = 0.4
elif Dam == 1 and 0.17 < chip_than <= 0.23:
    D_Offset = 0.5
elif Dam == 1 and 0.15 < chip_than <= 0.17:
    D_Offset = 0.55
elif Dam == 1 and 0.13 < chip_than <= 0.15:
    D_Offset = 0.65
elif Dam == 1 and 0.11 < chip_than <= 0.13:
    D_Offset = 0.75
elif Dam == 1 and 0.09 < chip_than <= 0.11:
    D_Offset = 0.85
elif Dam == 1 and chip_than <= 0.09:
    D_Offset = 1.1
elif Dam == 2 and chip_than > 0.5:
    D_Offset = 0.7
elif Dam == 2 and 0.3 < chip_than <= 0.5:
    D_Offset = 0.8
elif Dam == 2 and 0.23 < chip_than <= 0.3:
    D_Offset = 0.8
elif Dam == 2 and 0.17 < chip_than <= 0.23:
    D_Offset = 0.8
elif Dam == 2 and 0.15 < chip_than <= 0.17:
    D_Offset = 0.8
elif Dam == 2 and 0.13 < chip_than <= 0.15:
    D_Offset = 0.9
elif Dam == 2 and 0.11 < chip_than <= 0.13:
    D_Offset = 1
elif Dam == 2 and 0.09 < chip_than <= 0.11:
    D_Offset = 1.1
elif Dam == 2 and chip_than <= 0.09:
    D_Offset = 1.2
active_area = pow((D - D_Offset) / 2, 2) * 3.14
Chip_spacing = (active_area - chip_area * chip_sum) / chip_sum / 2
r = D / 2

# 打印输出结果
print('芯片面积=', "%.4f" % chip_area, )
print('芯片总数=', chip_sum)
print('镜面面积=', "%.4f" % D_area)
print('芯片比=', "%.4f" % chip_than)
print('镜面偏移=', "%.4f" % D_Offset)
print('有效面积=', "%.4f" % active_area)
print('芯片间距=', "%.4f" % Chip_spacing)
